package com.cypher.encryptionanddecryption.fragment.protocol.kunlun;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.cypher.encryptionanddecryption.R;
import com.cypher.encryptionanddecryption.activity.BluetoothCommunicationActivity;
import com.cypher.encryptionanddecryption.bean.CommunicationBean;
import com.cypher.encryptionanddecryption.fragment.protocol.AbstractProtocolFragment;
import com.cypher.encryptionanddecryption.util.AESUtils;
import com.cypher.encryptionanddecryption.util.HexUtils;
import com.cypher.encryptionanddecryption.util.KunLunProtocolUtils;
import com.cypher.encryptionanddecryption.util.Nb25ProtocolUtils;
import com.cypher.encryptionanddecryption.util.SimpleDateFormatUtils;
import com.cypher.encryptionanddecryption.watcher.ExpandDatePickerDialog;

import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * 按条件读取界面
 * @author: 古霸天
 * @date: 2020/11/16
 */
public class KunLunTimeQuantumFragment extends AbstractProtocolFragment {
    private EditText edit1;
    private EditText edit2;
    private EditText result;
    private final String EMPTY = "";
    private BluetoothCommunicationActivity activity;
    private CommunicationBean bean;


    public KunLunTimeQuantumFragment(BluetoothCommunicationActivity activity, CommunicationBean bean) {
        this.activity = activity;
        this.bean = bean;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_kunlun_time_quantum, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        init(view);
    }

    private void init(View view) {
        edit1 = view.findViewById(R.id.time_quantum_edit1);
        edit2 = view.findViewById(R.id.time_quantum_edit2);
        result = view.findViewById(R.id.time_quantum_result);
        // 定义输入框过滤器
        InputFilter[] inputFilters = new InputFilter[1];
        // 设置输入框输入内容长度
        inputFilters[0] = new InputFilter.LengthFilter(2);
        edit1.setFilters(inputFilters);
        edit1.setInputType(InputType.TYPE_CLASS_NUMBER);
        edit2.setFilters(inputFilters);
        edit2.setInputType(InputType.TYPE_CLASS_NUMBER);

    }

    @Override
    public void fillFragment(String data) {
        // 清空内容
        result.setText("");
        // 程序暂停0.2秒后开始渲染数据
        Runnable r = () -> {
            // 格式化数据
            String formatData = KunLunProtocolUtils.formatReceiveData(data);
            formatData = StringUtils.substring(formatData, 6);
            result.setText(formatResult(formatData));
        };
        activity.getHandler().postDelayed(r, 200);

    }

    @Override
    public String getHexCode(String rwType) {
        String edit1Str = edit1.getText().toString();
        String edit2Str = edit2.getText().toString();
        if (!isNotEmpty(edit1Str, "起始天数")) {
            return EMPTY;
        }
        if (!isNotEmpty(edit2Str, "记录条数")) {
            return EMPTY;
        }
        String edit1Code = getDays(Integer.parseInt(edit1Str));
        String edit2Code = getDays(Integer.parseInt(edit2Str));
        return  KunLunProtocolUtils.getKunLunSpecialCommand(bean.getHexCode() + edit1Code + edit2Code + "00");
    }

    /**
     * @param data
     * @return
     */
    private String formatResult(String data) {
        int split = 14;
        StringBuffer result = new StringBuffer();
        int count = data.length() / split;
        String temp, showData, unit;
        for (int i = 0; i < count; i++) {
            temp = data.substring(i * split, split * (i + 1));
            showData = temp.substring(0, 6);
            unit = HexUtils.formatDouble(
                    HexUtils.integerToDouble(Integer.parseInt(temp.substring(6, split), 16),
                            1), 1);
            result.append("20").append(temp.substring(0, 2)).append("年")
                    .append(temp.substring(2, 4)).append("月")
                    .append(temp.substring(4, 6)).append("日");
            result.append("           ").append(unit).append(" m³").append("\n");
        }
        return result.toString();
    }


    private boolean isNotEmpty(String edit3, String content) {
        if (StringUtils.isEmpty(edit3)) {
            Toast.makeText(activity, content + "不能为空！", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }
    private String getDays(Integer days) {
        return HexUtils.paddingLeft(Integer.toHexString(days), 2).toUpperCase();
    }

}
